package com.global.dao.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Repository;

import com.global.dao.DataSourceXColumnDao;
import com.global.model.DataSourceX;
import com.global.model.DataSourceXColumn;

@Repository("dataSourceXColumnDao")
public class DataSourceXColumnDaoImpl extends GenericDaoHibernate<DataSourceXColumn, Long> implements DataSourceXColumnDao {

	
	public DataSourceXColumnDaoImpl() {
		super(DataSourceXColumn.class);
	}

	/**
	 * {@inheritDoc}
	 */
	public DataSourceXColumn saveDataSourceXColumn(DataSourceXColumn dataSourceXColumn) {
		if(dataSourceXColumn!=null){
			if(log.isDebugEnabled()){
				log.debug("dataSourceXColumn's id :" + dataSourceXColumn.getId());
			}
			getHibernateTemplate().saveOrUpdate(dataSourceXColumn);
			getHibernateTemplate().flush();
		}

		return dataSourceXColumn;
	}
	
	/**
	 * {@inheritDoc}
	 */
	public void saveBatchDataSourceXColumns(List<DataSourceXColumn> saveList) {
		this.saveBatchDataSourceXColumns(saveList, null);
	}
	
	/**
	 * {@inheritDoc}
	 */
	public void saveBatchDataSourceXColumns(List<DataSourceXColumn> saveList, List<DataSourceXColumn> deleteList) {
		if(deleteList!=null){
			getHibernateTemplate().deleteAll(deleteList);
		}
		if(saveList!=null){
			getHibernateTemplate().saveOrUpdateAll(saveList);
		}
		
		getHibernateTemplate().flush();
	}
	
	/**
	 * {@inheritDoc}
	 */
	public DataSourceXColumn getDataSourceXColumn(String aliasName) {
		if(StringUtils.isNotBlank(aliasName)){
			HashMap<String, Object> queryParams = new HashMap<String, Object>();
	        queryParams.put("uniqueAlias", aliasName);
			List<DataSourceXColumn> list = findByNamedQuery("findDataSourceXColumnByAlias", queryParams);
			if(!list.isEmpty()){
				return list.get(0);
			}
		}

		return null;
	}

	/**
	 * {@inheritDoc}
	 */
	public List<DataSourceXColumn> getDataSourceXColumnList(DataSourceX dataSourceX) {
		if(dataSourceX!=null && dataSourceX.getId()!=null){
			HashMap<String, Object> queryParams = new HashMap<String, Object>();
	        queryParams.put("dataSourceXId", dataSourceX.getId());
	        
			return findByNamedQuery("findDataSourceXColumnByDataSourceX", queryParams);
		}
		
		return new ArrayList<DataSourceXColumn>();
	}
	

	/**
	 * {@inheritDoc}
	 */
	public void deleteDataSourceXColumn(DataSourceXColumn dataSourceXColumn) {
		getHibernateTemplate().delete(dataSourceXColumn);
		getHibernateTemplate().flush();
	}
	
	/**
	 * {@inheritDoc}
	 */
	public void deleteBatchDataSourceXColumns(List<DataSourceXColumn> dataSourceXColumnList) {
		this.saveBatchDataSourceXColumns(null, dataSourceXColumnList);
	}
	
	
	/**
	 * {@inheritDoc}
	 */
	@SuppressWarnings("rawtypes")
	public boolean isBinded(DataSourceXColumn dataSourceXColumn) {
		if(dataSourceXColumn!=null){
			StringBuffer hqlBuffer = new StringBuffer();
			hqlBuffer.append("select id from GridColumn g \n");
			hqlBuffer.append(" where g.dataSourceXColumn.id = :dataSourceXColumnId \n");
			hqlBuffer.append(" and rownum = 1 \n");
			
			List list = getHibernateTemplate().findByNamedParam(hqlBuffer.toString(), "dataSourceXColumnId", dataSourceXColumn.getId());
			
			return !list.isEmpty();
		}
		
		return false;
	}
}
